﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	public partial class FeedBack : ICreator { }




	/// <summary>
	/// FeedBacks 类。
	/// </summary>
	public static partial class FeedBacks
	{
		public const string INFO_TYPE = "FeedBack";



		#region GetItem
		/// <summary>
		/// 根据标识获取指定的反馈。
		/// </summary>
		/// <param name="o">反馈集合。</param>
		/// <param name="feedBackId">获取的反馈标识 FeedBackId。</param>
		/// <returns>获取的信息项。</returns>
		public static FeedBack GetItem(this System.Data.Linq.Table<FeedBack> o, int feedBackId)
		{
			return o.SingleOrDefault(p => p.FeedBackId == feedBackId);
		}

		public static FeedBack GetItem(int feedBackId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.FeedBacks.GetItem(feedBackId);
		}
		#endregion



		#region Exists
		/// <summary>
		/// 验证指定的反馈是否已经存在。
		/// </summary>
		/// <param name="o">反馈集合。</param>
		/// <param name="feedBackId">验证的反馈标识 FeedBackId。</param>
		/// <returns>布尔值表示指定的反馈是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<FeedBack> o, int feedBackId)
		{
			return o.Any(p => p.FeedBackId == feedBackId);
		}
		#endregion
	}



	/// <summary>
	/// FeedBacks 类操作部分。
	/// </summary>
	public static partial class FeedBacks
	{
		#region Post
		/// <summary>
		/// 提交反馈。
		/// </summary>
		/// <param name="postedFeedBack">提交的反馈。</param>
		/// <returns>提交的反馈。</returns>
		public static FeedBack Post(FeedBack postedFeedBack)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			FeedBack feedBack = postedFeedBack.ToNewEntity<FeedBack>();
			feedBack.CreatorId = Users.CurrentUserId;
			feedBack.CreationDate = DateTime.Now;

			GetPostRight(dc, feedBack, true);	// 权限验证

			dc.FeedBacks.InsertOnSubmit(feedBack);
			dc.SubmitChanges();

			// 记录日志
			Logs.Insert(dc, ResourceId, (int)PermissionCode.Post, INFO_TYPE, feedBack.FeedBackId.ToString(), PermissionCode.Post.ToString(), StringExtensions.CutString(feedBack.Summary, 300, "..."));

			// 返回只读数据
			return postedFeedBack.FillByEntity(feedBack);
		}
		#endregion



		#region Delete
		/// <summary>
		/// 删除反馈。
		/// </summary>
		/// <param name="feedBackId">删除的反馈标识 FeedBackId。</param>
		public static void Delete(int feedBackId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			FeedBack feedBack = dc.FeedBacks.GetItem(feedBackId);
			if (feedBack == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetDeleteRight(dc, feedBack, true);	// 权限验证

			Delete(dc, feedBack);

			dc.SubmitChanges();

			// 记录日志
			Logs.Insert(dc, ResourceId, (int)PermissionCode.Delete, INFO_TYPE, feedBack.FeedBackId.ToString(), PermissionCode.Delete.ToString(), StringExtensions.CutString(feedBack.Summary, 300, "..."));
		}

		#region internal Delete
		internal static void Delete(GenericModuleDataContext dc, FeedBack feedBack)
		{
			dc.FeedBacks.DeleteOnSubmit(feedBack);
		}
		#endregion
		#endregion



		#region 注册反馈字典类别
		public static readonly DicClass FeedBackDicClass = RegisterDicClass();

		public const int FeedBackDicClassId = 20;
		public const string FeedBackDicClassCode = "InfoFeedBackType";
		/// <summary>
		/// 注册反馈字典类别。
		/// </summary>
		public static DicClass RegisterDicClass()
		{
			DicClass info = new DicClass();
			info.ClassId = FeedBackDicClassId;
			info.ClassName = "信息反馈类别";
			info.ClassCode = FeedBackDicClassCode;
			info.Memo = "";
			info.SiteId = null;

			return DicClasses.Register(info);
		}
		#endregion
	}



	/// <summary>
	/// FeedBacks 类对应操作权限部分。（受限站点资源。）
	/// </summary>
	public static partial class FeedBacks
	{
		#region GetPostRight
		/// <summary>
		/// 获取当前用户是否有提交反馈的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="feedBack">提交的反馈对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有提交反馈的权限。</returns>
		public static bool GetPostRight(GenericModuleDataContext dc, FeedBack feedBack, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)PermissionCode.Post;
			bool b = dc.AccessController.GetUserIsHasPermission<FeedBack>(userName, feedBack, ResourceId, permissionCode, feedBack.SiteId);

			if (triggerException && !b)
				throw new PermissionException(userName, feedBack, ResourceId, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有提交反馈的权限。
		/// </summary>
		/// <param name="feedBack">提交的反馈对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有提交反馈的权限。</returns>
		public static bool GetPostRight(FeedBack feedBack, bool triggerException)
		{
			return GetPostRight(new GenericModuleDataContext(), feedBack, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有提交反馈的权限。
		/// </summary>
		/// <param name="feedBack">提交的反馈对象。</param>
		/// <returns>布尔值表示当前用户是否有提交反馈的权限。</returns>
		public static bool GetPostRight(FeedBack feedBack)
		{
			return GetPostRight(feedBack, false);
		}
		#endregion



		#region GetDeleteRight
		/// <summary>
		/// 获取当前用户是否有删除反馈的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="feedBack">删除的反馈对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有删除反馈的权限。</returns>
		public static bool GetDeleteRight(GenericModuleDataContext dc, FeedBack feedBack, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)PermissionCode.Delete;
			bool b = dc.AccessController.GetUserIsHasPermission<FeedBack>(userName, feedBack, ResourceId, permissionCode, feedBack.SiteId);

			if (triggerException && !b)
				throw new PermissionException(userName, feedBack, ResourceId, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有删除反馈的权限。
		/// </summary>
		/// <param name="feedBack">删除的反馈对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有删除反馈的权限。</returns>
		public static bool GetDeleteRight(FeedBack feedBack, bool triggerException)
		{
			return GetDeleteRight(new GenericModuleDataContext(), feedBack, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有删除反馈的权限。
		/// </summary>
		/// <param name="feedBack">删除的反馈对象。</param>
		/// <returns>布尔值表示当前用户是否有删除反馈的权限。</returns>
		public static bool GetDeleteRight(FeedBack feedBack)
		{
			return GetDeleteRight(feedBack, false);
		}
		#endregion
	}




	/// <summary>
	/// FeedBacks 类后台管理部分。
	/// </summary>
	public static partial class FeedBacks
	{
		public static bool GetManageListRight()
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			return dc.AccessController.GetUserIsHasPermission<object>(Users.CurrentUserName, null, FeedBacks.ResourceId, (int)FeedBacks.PermissionCode.Delete, Sites.CurrentSiteId);
		}

		public static bool GetManageListRight(int? siteId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			return dc.AccessController.GetUserIsHasPermission<object>(Users.CurrentUserName, null, FeedBacks.ResourceId, (int)FeedBacks.PermissionCode.Delete, siteId);
		}




		public struct FeedBackForManageList
		{
			public int FeedBackId { get; set; }
			public int? SiteId { get; set; }
			public string InfoType { get; set; }
			public string Summary { get; set; }
			public string Opinion { get; set; }
			public string UserIp { get; set; }
			public DateTime CreationDate { get; set; }
		}

		public static PaginationInfo<List<FeedBackForManageList>> GetPaginationManageList(int? siteId, string infoType, DateTime? startTime, DateTime? endTime, string keyword, int pageSize, int currentPage)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;

			var q = from p in dc.FeedBacks
					orderby p.CreationDate descending
					select new FeedBackForManageList { FeedBackId = p.FeedBackId, SiteId = p.SiteId, InfoType = p.InfoType, Summary = p.Summary, Opinion = p.Opinion, UserIp = p.UserIp, CreationDate = p.CreationDate };

			if (siteId.HasValue)
				q = q.Where(p => p.SiteId == siteId.Value);

			if (!string.IsNullOrEmpty(infoType))
				q = q.Where(p => p.InfoType == infoType);

			if (startTime.HasValue)
				q = q.Where(p => p.CreationDate >= startTime.Value);

			if (endTime.HasValue)
				q = q.Where(p => p.CreationDate <= endTime.Value);

			if (!string.IsNullOrEmpty(keyword))
				q = q.Where(p => p.Summary.Contains(keyword) || p.Opinion.Contains(keyword));

			PaginationInfo<List<FeedBackForManageList>> pageInfo = new PaginationInfo<List<FeedBackForManageList>>();
			pageInfo.PageSize = pageSize;
			pageInfo.CurrentPage = currentPage;
			pageInfo.RecordCount = q.Count();
			pageInfo.Records = q.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
			return pageInfo;
		}
	}
}